/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package home.ex.todolist.server;

import home.ex.todolist.shared.Todo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

/**
 * 
 * @author peerawatpoombua
 */
class TodoServiceImpl implements TodoService {
    
    public TodoServiceImpl() {
    }
    
    @Override
    public void create(final Todo todo) {
	final Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	session.save(todo);
	session.getTransaction().commit();
    }
    
    @Override
    public Todo getTodo(final int id) {
	final Session session = HibernateUtil.getSessionFactory().openSession();
	session.beginTransaction();
	final List result = session.createQuery("from Todo").list();
	session.getTransaction().commit();
	return (Todo) result.get(0);
    }
    
    @Override
    public ArrayList<Todo> getAllTodo() {
	final ArrayList<Todo> todos = new ArrayList<Todo>();
	todos.add(new Todo("HomeWork", false));
	todos.add(new Todo("Release #1", false));
	return todos;
    }
}
